﻿package com.ehouse.models;

import java.util.HashMap;
import java.util.Map.Entry;

public class Rule {
	/**
	 * Id of the rule
	 */
	private String id;
	/**
	 * Time in which the rule will be executed, in hh:mm format
	 */
	private String time;
	/**
	 * Data of the rule
	 */
	private Object data;
	/**
	 * Formatted rule ready to be sent as a command
	 */
	private String formatted;

	public Rule(String id, String time, String formatted, Object data) {
		this.id = id;
		this.time = time;
		this.data = data;
		this.formatted = formatted;
	}

	public String getId() {
		return id;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getFormatted() {
		return formatted;
	}

	public void setFormatted(String formatted) {
		this.formatted = formatted;
	}

	@SuppressWarnings("unchecked")
	public String toString() {
		if (data instanceof String) {
			return (String) data + "ºC";
		} else if (data instanceof Boolean) {
			return ((Boolean) data ? "Turn ON" : "Turn OFF");
		}
		HashMap<String, Boolean> dict = (HashMap<String, Boolean>) data;
		String desc = "Turn " + (dict.get("VALUE") ? "ON" : "OFF")
				+ " these lights: ";
		for (Entry<String, Boolean> each : dict.entrySet()) {
			if (each.getKey() == "VALUE" || !each.getValue()) {
				continue;
			}
			desc += each.getKey() + "; ";
		}
		return desc;
	}
}
